package com.chixing.controller;

import com.chixing.entity.Customer;
import com.chixing.service.AddCustomerService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.stream.Collectors;


@RestController

public class AddCustomerController  {
    @Autowired
    private AddCustomerService addCustomerService;

    @GetMapping("customer/getId/{id}/get_info")
    public ModelAndView getById(@PathVariable("id")Integer custId){
        ModelAndView mav = new ModelAndView();
       Customer customer = addCustomerService.getById(custId);
        mav.addObject("customer",customer);
        mav.setViewName("customer/personalCenter-UserInformation");
       return mav;

    }

    @PostMapping("customer/getId/{id}/get_info/save")
    @ResponseBody
    public ModelAndView save(@PathVariable("id") Integer custId, HttpServletRequest request){
        String addEmail = request.getParameter("addEmail");
        String addTelStr = request.getParameter("addTel");
        String addName = request.getParameter("addName");
        String addCertificatesStr = request.getParameter("addCertificates");
        String addCertificatesNumberStr = request.getParameter("addCertificatesNumber");
        String addBirStr = request.getParameter("addBir");
        String addDrive = request.getParameter("addDrive");
        Long addTel = Long.valueOf(addTelStr);
        Integer addCertificates = Integer.valueOf(addCertificatesStr);
        Long addCertificatesNumber = Long.valueOf(addCertificatesNumberStr);
        LocalDate addBir = LocalDate.parse(addBirStr , DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Customer cust = new Customer();
        cust.setCustEmail(addEmail);
        cust.setCustTelno(addTel);
        cust.setCustName(addName);
        cust.setCustCertificates(addCertificates);
        cust.setCustCertificatesNumber(addCertificatesNumber);
        cust.setCustBirthday(addBir);
        cust.setCustDriverLicense(addDrive);
        System.out.println("修改后的用户信息"+cust);
        Integer rows = addCustomerService.updateCust(custId,cust);
        Result result =  Result.success("ok",rows);
        ModelAndView mav = new ModelAndView();
        mav.addObject("customer",cust);
        mav.setViewName("redirect:/customer/getId/"+custId+"/get_info");
        return mav;
    }

}
